from django.contrib.auth.decorators import login_required
from django.shortcuts import render

from apps.career.models import Careerplan
from apps.home.models import *
from apps.message.models import Comments
from apps.setup.models import Timeslot, Experts
from utils import restful
from .serializers import ProresultSerializers, UsermedalSerializers


@login_required(login_url='/account/login/')
def Index(request):
    return render(request, 'home/index.html')


# 首页信息卡片
def count(request):
    user_id = request.session['user_id']
    careerplan = Careerplan.objects.filter(user_id=user_id)
    careerplanCount = careerplan.count()
    draftsCount = careerplan.filter(status='0').count()
    timesLotCount = Timeslot.objects.count()
    expertsCount = Experts.objects.count()
    commentsCount = Comments.objects.filter(user_id=user_id).count()
    data = {
        'careerplanCount':careerplanCount,
        'draftsCount':draftsCount,
        'timesLotCount':timesLotCount,
        'expertsCount':expertsCount,
        'commentsCount':commentsCount
    }
    return restful.result(data=data,message=None)

# 可视化数据
def selectProresult(request):
    if request.is_ajax() and request.method == "POST":
        data = request.POST
        questionType_Id = data.get('questionType_Id')
        user_id = data.get('user_Id')
        datas = Proresult.objects.filter(questiontype=questionType_Id, user_id=user_id)
        data = ProresultSerializers(datas, many=True).data
        if data:
            return restful.result(data=data, message='登录成功！')
        else:
            return restful.params_error(message=None)
    return restful.method_error(message='请求出错')


# 徽章
def tags(request):
    user_id = request.session.get('user_id')
    # 请求徽章
    if request.is_ajax() and request.method == "GET":
        medal_ids = Usermedal.objects.filter(user=user_id)
        data = UsermedalSerializers(medal_ids, many=True).data

        # medal_id_list = medal_ids.values('medal_id')
        # medals = Medals.objects.filter(medal_id__in=medal_id_list)
        # data = list(medals.values())

        return restful.ok(data=data)

    # 添加徽章
    if request.is_ajax() and request.method == "POST":
        medalType_id = int(request.POST.get('medalType_id'))
        try:
            medalName = request.POST.get('medalName')
        except:
            return restful.params_error(message='请勿添加相同的徽章！')
        exist = Medals.objects.filter(medalname=medalName).first()
        if exist:
            try:
                usermedal = Usermedal(user_id=user_id, medal_id=exist.medal_id)
                usermedal.save()
            except:
                return restful.params_error(message='已存在，添加失败！')
        else:
            medals = Medals(medaltype_id=medalType_id, medalname=medalName)
            medals.save()
            usermedal = Usermedal(user_id=user_id, medal_id=medals.medal_id)
            usermedal.save()
        return restful.ok(message='添加成功！')

    return restful.method_error(message='请求出错！')

# 删除徽章
def deleteTag(request):
    if request.is_ajax() and request.method == "POST":
        medalName = request.POST.get('medalName')
        try:
            medal = Medals.objects.get(medalname=medalName)
            medal.delete()
            return restful.ok(message='删除成功！')
        except:
            pass
    return restful.method_error('请求出错！')
